home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
3_0
/
MDEF__
/
PATMENUD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-01-02
|
8KB
|
427 lines
#include "patmenudemo.h"
/* Globals */
WindowPtr aWindow;
MenuHandle AppleMenu;
MenuHandle FileMenu;
MenuHandle EditMenu;
MenuHandle GraphicMenu;
Pattern testPattern;
EventRecord theEvent;
Boolean Finished;
Boolean quitonwindclose;
main()
{
Init();
makemenus();
makewindow();
Finished = FALSE;
/* the event loop */
while (Finished==FALSE){
SystemTask();
if (GetNextEvent(everyEvent,&theEvent))
handleanevent();
}
SetCursor(*(CursHandle)GetCursor(watchCursor));
zoomport(aWindow,FALSE);
}
GracefulExit()
{
/*
just return to Finder on system errors
*/
ExitToShell();
}
Init()
{
int index;
MaxApplZone();
for (index=1;index<11;index++)
MoreMasters();
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent,0);
InitWindows();
InitMenus();
TEInit();
InitDialogs(GracefulExit);
InitCursor();
GetIndPattern(&testPattern,MYPATLIST,0);
quitonwindclose = TRUE; /* closing application window exits application */
randSeed = TickCount();
}
abs(x)
int x;
{
/*
this could have been a macro except when used for Random(),
ie, if it were defined as
#define abs(x) (x)>0?(x):-(x)
then abs(Random()) would expand to
(Random())>0?(Random()):-(Random())
returning unpredictable results
*/
if (x<0) return (-x);
else return(x);
}
makemenus()
{
AppleMenu = GetMenu(AppleID);
AddResMenu(AppleMenu,(ResType)'DRVR');
InsertMenu(AppleMenu,0);
FileMenu = GetMenu(FileID);
InsertMenu(FileMenu,0);
EditMenu = GetMenu(EditID);
InsertMenu(EditMenu,0);
UpdateEdit(FALSE);
GraphicMenu = NewMenu(GraphicID,"\pPattern");
/*
set the MenuHandle.menuProc to the MDEF #130 in our
resource file
*/
(*GraphicMenu)->menuProc = GetResource('MDEF',130);
/*
calculate how large the menu rectangle is
*/
CalcMenuSize(GraphicMenu);
InsertMenu(GraphicMenu,0);
DrawMenuBar();
}
UpdateEdit(OnOff)
Boolean OnOff;
{
int index;
/*
if OnOff is true, then enable Edit menu items, otherwise
disable all of the Edit menu items.
*/
for(index=1;index<7;index++)
if (OnOff)
EnableItem(EditMenu,index);
else
DisableItem(EditMenu,index);
DisableItem(EditMenu,2);
}
makewindow()
{
Rect wRect;
FontInfo FontStuff;
/*
make an application window
*/
SetRect(&wRect,0,0,220,150);
SetPort(aWindow = NewWindow(nil,&wRect,"\pPattern Demo",FALSE,8,(WindowPtr)-1,TRUE,0));
TextFont(0);
TextSize(12);
TextFace(0);
TextMode(srcCopy);
centerwindow(aWindow,&screenBits.bounds);
zoomport(aWindow,TRUE);
}
odd(theLong)
long theLong;
{
/*
this one could probably have been defined as a macro
*/
return(theLong % 2);
}
DoMenu(mChoice)
long mChoice;
{
int MenuID,MenuItem;
char accName[256];
MenuID = HiWord(mChoice);
MenuItem = LoWord(mChoice);
switch (MenuID){
case AppleID: switch (MenuItem){
case 1: doabout();
break;
default: GetItem(AppleMenu,MenuItem,accName);
(void)OpenDeskAcc(accName);
break;
}
break;
case FileID: switch (MenuItem){
case QuitItem: Finished = TRUE;
break;
default: break;
}
break;
case EditID: if (!SystemEdit(MenuItem-1)){
switch(MenuItem){
case UndoItem: break;
case CutItem: break;
case CopyItem: break;
case PasteItem: break;
case ClearItem: break;
default: break;
}
}
break;
case GraphicID: {
GrafPtr tempPort;
if (MenuItem > 0){
GetIndPattern(&testPattern,MYPATLIST,MenuItem);
GetPort(&tempPort);
SetPort(aWindow);
InvalRect(&aWindow->portRect);
SetPort(tempPort);
}
}
break;
}
HiliteMenu(0);
}
DoDrag(aPoint,eventWindow)
Point aPoint;
WindowPtr eventWindow;
{
/*
drag the application window around within screenBits.bounds
If eventWindow != FrontWindow(), don't bring it to front, this
way a command-drag can move a background window without
bringing it to the front, according to the Macintosh user
interface guidelines
*/
DragWindow(eventWindow, aPoint, &screenBits.bounds);
}
DoContent(aPoint,eventWindow)
Point aPoint;
WindowPtr eventWindow;
{
int thePart;
register int index,pole;
ControlHandle theControl;
Rect cRect;
if (FrontWindow() != eventWindow)
SelectWindow(eventWindow);
else{
GlobalToLocal(&aPoint);
/*
handle any application-specific mousedown events here
*/
}
}
dogrow(aPoint,eventWindow)
Point aPoint;
WindowPtr eventWindow;
{
long newSize;
int newWidth,newHeight;
Rect limitRect;
if (FrontWindow() != eventWindow)
SelectWindow(eventWindow);
else{
/*
set a limit rectangle for minimum and maximum window size
*/
SetRect(&limitRect,100,100,32700,32700);
newSize = GrowWindow(eventWindow,aPoint,&limitRect);
if (newSize != nil){
newWidth = LoWord(newSize);
newHeight = HiWord(newSize);
SizeWindow(eventWindow,newWidth,newHeight,TRUE);
InvalRect(&eventWindow->portRect);
}
}
}
DoGoAway(aPoint,eventWindow)
Point aPoint;
WindowPtr eventWindow;
{
/*
if the global Boolean "quitonwindclose" is set to true, then
closing the application window will also exit the application.
otherwise, the window just goes away.
*/
if (TrackGoAway(eventWindow,aPoint))
Finished = quitonwindclose;
}
handlemousedown()
{
Point mouseWhere;
WindowPtr whichWindow;
int Part;
/*
we have a mousedown event, find out where the mouse was
clicked, and dispatch to proper mousedown handling routines
*/
switch (Part = FindWindow(theEvent.where,&whichWindow)){
case inSysWindow: SystemClick(&theEvent,whichWindow);
break;
case inMenuBar: DoMenu(MenuSelect(theEvent.where));
break;
case inDrag: DoDrag(theEvent.where,whichWindow);
break;
case inContent: DoContent(theEvent.where,whichWindow);
break;
case inGrow: dogrow(theEvent.where,whichWindow);
break;
case inGoAway: DoGoAway(&theEvent.where,whichWindow);
break;
case inZoomIn:
case inZoomOut:
if (TrackBox(whichWindow,theEvent.where,Part)){
ZoomWindow(whichWindow, Part, TRUE);
InvalRect(&whichWindow->portRect);
}
break;
default: break;
}
}
handlekeydown()
{
char ch;
int chCode;
long menuChoice,newTime;
Rect charRect;
ch = (char)theEvent.message & charCodeMask;
/*
was the command-key down? If so, it might be a command key
equivalent for a menu item
*/
if ((theEvent.modifiers & cmdKey) !=0)
DoMenu(MenuKey(ch));
else{
/* application-dependent key presses handled here */
}
}
handleactivate()
{
WindowPtr eventWindow;
eventWindow = (WindowPtr)theEvent.message;
SetPort(eventWindow);
if (eventWindow==aWindow){
DrawGrowIcon(eventWindow);
if (BitAnd(theEvent.modifiers,activeFlag)!=0){
/*
our window just got an activate event
*/
if (BitAnd(theEvent.modifiers,changeFlag) !=0)
UpdateEdit(FALSE);
}
else{
/*
our window just got a deactivate event
*/
if (BitAnd(theEvent.modifiers,changeFlag) !=0)
UpdateEdit(TRUE);
}
}
}
handleupdate()
{
GrafPtr tempPort,thisPort;
Rect dummyRect;
GetPort(&tempPort);
thisPort = (WindowPtr)theEvent.message;
SetPort(thisPort);
if (thisPort==aWindow){
BeginUpdate(thisPort);
EraseRect(&thisPort->portRect);
DrawGrowIcon(thisPort);
/*
clip drawing to exclude the scroll bar areas
since our window has a growicon
*/
dummyRect = thisPort->portRect;
dummyRect.right -= 15;
dummyRect.bottom -= 15;
ClipRect(&dummyRect);
/*
our application-specific window drawing routines
*/
FillRect(&thisPort->portRect,testPattern);
/*
after drawing, set the clip region to an
arbitrarily large rectangle
*/
SetRect(&dummyRect,-32000,-32000,32000,32000);
ClipRect(&dummyRect);
EndUpdate(thisPort);
}
SetPort(tempPort);
}
handleanevent()
{
/*
find out what kind of event occured, and dispatch to the
proper event-handling routines
*/
switch (theEvent.what){
case mouseDown: handlemousedown();
break;
case keyDown:
case autoKey: handlekeydown();
break;
case activateEvt: handleactivate();
break;
case updateEvt: handleupdate();
break;
}
}